package com.andeveloper.kyodai;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.StretchViewport;

public class SelectLevelScreen implements Screen {
	private ImageButton back;
	private Image bg;
	private TextButton[] buttons;
	private Kyodai game;
	private Group levelButtons;
	private TextButton.TextButtonStyle loseFlagStyle;
	private Stage stage;
	private TextButton.TextButtonStyle winFlagStyle;
	private Label label;
	public SelectLevelScreen(Kyodai game) {
		this.game = game;
		this.levelButtons = new Group();
		NinePatch localNinePatch1 = new NinePatch(Assets.blueBall);
		NinePatch localNinePatch2 = new NinePatch(Assets.orangeBall);
		BitmapFont localBitmapFont = new BitmapFont(
				Gdx.files.internal("font/kyodai.fnt"),
				Gdx.files.internal("font/kyodai.png"), false);
		this.winFlagStyle = new TextButton.TextButtonStyle(new NinePatchDrawable(localNinePatch2), new NinePatchDrawable(localNinePatch2), new NinePatchDrawable(localNinePatch2), localBitmapFont);
		this.loseFlagStyle = new TextButton.TextButtonStyle(new NinePatchDrawable(localNinePatch1), new NinePatchDrawable(localNinePatch1), new NinePatchDrawable(localNinePatch1), localBitmapFont);
		this.buttons = new TextButton[GameConfig.levelFlag.length];
		this.bg = new Image(new NinePatch(Assets.bg, 10, 10, 20, 20));
		this.bg.setHeight(GameConfig.SCREEN_HEIGHT);
		this.bg.setWidth(GameConfig.SCREEN_WIDTH);
		this.back = new ImageButton(new TextureRegionDrawable(Assets.back));
		this.back.setX(GameConfig.SCREEN_WIDTH / 2 - Assets.back
				.getRegionWidth() / 2);
		this.back.setY(20.0F);
		this.back.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				click(SelectLevelScreen.this.back);
			}
		});

		BitmapFont localBitmapFont2 = new BitmapFont(
				Gdx.files.internal("font/kyodai.fnt"),
				Gdx.files.internal("font/kyodai.png"), false);
		this.label = new Label("关卡", new Label.LabelStyle(localBitmapFont2,
				new Color(0.0F, 1.0F, 1.0F, 1.0F)));

		this.stage = new Stage(new StretchViewport(GameConfig.SCREEN_WIDTH, GameConfig.SCREEN_HEIGHT));
		this.stage.addActor(this.bg);
		this.stage.addActor(this.back);
		final int i = (1 + GameConfig.levelFlag.length / 4) / 2;
		int j = GameConfig.SCREEN_HEIGHT / 2 + i * 104;
		int k = -188 + GameConfig.SCREEN_WIDTH / 2;
		for (int m = 0; m < GameConfig.levelFlag.length; ++m) {
			if (GameConfig.levelFlag[m]) {
				this.buttons[m] = new TextButton((m + 1) + "",
						this.winFlagStyle);
			} else {
				this.buttons[m] = new TextButton((m + 1) + "",
						this.loseFlagStyle);
			}
			this.buttons[m].setX(k + 104 * (m % 4));
			this.buttons[m].setY(j - 104 * (m / 4));
			final int idx = m;
			this.buttons[m].addListener(new ClickListener() {
				@Override
				public void clicked(InputEvent event, float x, float y) {
					click(SelectLevelScreen.this.buttons[idx]);
				}
			});
			this.levelButtons.addActor(this.buttons[m]);
		}
		this.stage.addActor(this.levelButtons);
		this.label.setX(GameConfig.SCREEN_WIDTH / 2 - this.label.getWidth() / 2.0F);
		this.label.setY(50.0F + (this.buttons[0].getY() + this.buttons[0].getHeight()));
		this.stage.addActor(this.label);
	}

	public void click(Actor paramActor) {
		if (paramActor == this.back) {
			getGame().setScreen(getGame().getMainMenuScreen());
		} else {
			TextButton localTextButton = (TextButton) paramActor;
			int i = -1 + Integer.parseInt(localTextButton.getText().toString());
//			if (GameConfig.levelFlag[i]) {
				GameScreen localGameScreen = getGame().getGameScreen();
				localGameScreen.resetGame(i);
				getGame().setScreen(localGameScreen);
//			}
		}
		if (GameConfig.sound) {
			Assets.sel.play();
		}
	}

	public void dispose() {
		this.stage.dispose();
	}

	public void hide() {
//		this.levelButtons.clear();
//		this.stage.clear();
	}

	public void pause() {
	}

	public void render(float paramFloat) {
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		this.stage.act(Gdx.graphics.getDeltaTime());
		this.stage.draw();
	}

	public void resize(int paramInt1, int paramInt2) {
	}

	public void resume() {
	}

	public void show() {
//		game.getApp().setAdViewVisibility(true);
		for (int m = 0; m < GameConfig.levelFlag.length; ++m) {
			if (GameConfig.levelFlag[m]) {
				this.buttons[m].setStyle(this.winFlagStyle);
			} else {
				this.buttons[m].setStyle(this.loseFlagStyle);
			}
		}
		Gdx.input.setInputProcessor(this.stage);
	}

	public Kyodai getGame() {
		return game;
	}
}
